﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teamboard.Properties;
using Teamboard.Models;
using System.Linq;
using Teamboard.TeamCity;
using Teamboard.Helpers;

namespace Teamboard.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        TeamCityHelper tcHelper = new TeamCityHelper();
        SettingsManager _settings = new SettingsManager();

        public ActionResult Index()
        {
            var projects = tcHelper.GetDashBoardData();

            var runningBuilds = tcHelper.Client.BuildsByBuildLocator(TeamCitySharp.Locators.BuildLocator.RunningBuilds());
            if (runningBuilds != null)
            {

                foreach (var runningBuild in runningBuilds)
                {
                    foreach (var project in projects)
                    {
                        foreach (var config in project.Configurations)
                        {
                            if (config.Id == runningBuild.BuildTypeId)
                            {
                                config.Status = "RUNNING";
                                config.LatestBuildNumbers.Add(runningBuild.Number);
                            }
                        }
                    }
                }
            }

            return View(projects);
        }

        public ActionResult Configure()
        {
            var projects = tcHelper.GetProjectsAndConfigurations();
            return View(projects);
        }
    }
}
